<template>
	<view>
		<h-comment-box v-if="commentList.length !== 0" :list="commentList" @getLike="getLike" @sendComment="sendComment"
			v-model="inputVal" @getMore="getMore" ref="commontRef" @remove="remove"
			@updateModelValue="updateModelValue"></h-comment-box>
	</view>
</template>

<script>
	import hCommentBox from '@/components/h-comment-box/h-comment-box.vue'
	import Vue from "vue";
	export default {
		components: {
			hCommentBox
		},
		data() {
			return {
				pageForm: {
					current: 1,
					pages: 0,
					size: 3,
					communityId: null

				},
				replay_form: {

				},
				inputVal: "", // 评论弹框的value
				userInfo: {
					id: 999,
					name: 'h_comment',
					url: "https://cdn.uviewui.com/uview/template/SmilingDog.jpg",
					contentStr: '',
					isLike: false,
					likeNum: 0,
					date: "2023-06-01 12:00",
				},
				commentList: [

				],
				replyList: []
			}
		},
		methods: {
			/**
			 * data 一级评论索引 *  二级评论索引
			 */
			getLike(data, callback) {

				let form = {
					//当前评论id
					commentId: data.id
				}

				uni.getStorage({
					key: "like",
					success: function(res) {
						// 获取到原有的值
						const oldValue = res.data;
						let mergedValue;
						if (Array.isArray(oldValue)) {
							// 如果原有的值是数组，则将新的值添加到数组中
							mergedValue = [...new Set([...oldValue, form.commentId])];
						} else {
							// 如果原有的值不是数组，则创建一个新的数组，并将原有的值和新的值都添加进去
							mergedValue = [oldValue, form.commentId];
						}

						// 存入缓存
						uni.setStorage({
							key: "like",
							data: mergedValue,
							success: function() {
								// 获取到token后，可以将其作为参数传递给api.ClickLike()方法
								const result = Vue.prototype.$u.api.ClickLike(form);

								callback(); // 点赞成功后调用该方法 更新点赞状态
							}
						});
					},
					fail: function() {
						// 缓存中不存在原有的值
						// 直接存入新的值
						uni.setStorage({
							key: "like",
							data: form.commentId,
							success: function() {
								// 获取到token后，可以将其作为参数传递给api.ClickLike()方法
								const result = Vue.prototype.$u.api.ClickLike(form);

								callback(); // 点赞成功后调用该方法 更新点赞状态
							}
						});
					}
				});






			},
			async sendComment(data, callback) {

				let params = {

				}

				let form = {

				}
				// 判断是否有回复的人，有的话，就是回复的回复，没有就是回复的一级评论
				if (data.row_s.id) {
					console.log(data.row_s.userId);
					// params.to_user_id = data.row_s.userId
					// params.to_user_name = data.row_s.name
					form = {
						communityId: this.pageForm.communityId,
						parentUsername: data.row_s.parentUsername,
						parentUserId: data.row_s.userId,
						parentCommentId: data.row_s.id,
						commentContent: this.inputVal,
						floorCommentId: data.row.id
					}
				} else {
					form = {
						communityId: this.pageForm.communityId,
						parentUsername: data.row.parentUsername,
						parentUserId: data.row.userId,
						parentCommentId: data.row.id,
						commentContent: this.inputVal,
					}
				}


				// 评论接口发送成功后，把当前评论的信息传给 callback(新的评论信息，接口返回) 用于更新评论列表
				const result = await this.$u.api.sendComment(form)


				callback(params)


				// 关闭评论框
				this.$refs.commontRef.close();
			},
			async getMore(data, callback) {
				
				
				
				// 调用 获取更多评论接口，成功后调用 callback(【新的评论信息，接口返回，可是对象】) 用于更新评论列表
				console.log(data, 'data');
				//通过data.row查找出楼层id
				
				

				this.replay_form = {
					current: 1,
					pages: 0,
					size: 2,
					communityId: this.pageForm.communityId,
					floorCommentId: data.row.id
				}

				if (data.row.id !== this.replay_form.floorCommentId || data.commentListLength === 0) {
					this.replay_form.current = 0
				}

				this.replay_form.current++

				const reply_result = await this.$u.api.getCommentByCommunityId(this.replay_form)
				this.replay_form.pages = reply_result.pages

				callback(reply_result.records)

			},
			remove(data, callback) {
				// 调用删除接口
				console.log(data);

				// 调用删除接口成功后，调用 callback() 用于更新评论列表
				callback()
			},

			updateModelValue(e) {

				this.inputVal = e
			}

		},
		created() {



		},

		onLoad: async function(option) {

			this.pageForm.communityId = option.communityId
			//查询出评论
			const result = await this.$u.api.getCommentByCommunityId(this.pageForm)

			this.commentList = result.records


		}
	}
</script>

<style>

</style>